{% extends "managersys/base/base_page.html" %}
{% load staticfiles %}
{% block content %}
    <div class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="content">
                            <h4 class="title">{{ title }}</h4>

                            <div class="toolbar">
                                <div class="row">
                                    <div class="content">
                                        <div class="col-md-4 p-5">
                                            <button onclick="delete_info('all')" class="btn btn-danger btn-fill btn-wd">
                                                清空
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="material-datatables">
                                <table id="datatables" class="table table-striped table-no-bordered table-hover">
                                    <thead>
                                    <tr>
                                        <th class="text-center">ID</th>
                                        <th class="text-center">请求时间</th>
                                        <th class="disabled-sorting text-center">IP地址</th>
                                        <th class="text-center">城市</th>
                                        <th class="disabled-sorting text-center">城市ID</th>
                                        <th class="text-center">请求设备</th>
                                        <th class="disabled-sorting text-center">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody id="datacontent">
                                    {% for pv_info in pv_infos %}
                                        <tr id="pv_info_{{ pv_info.id }}">
                                            <td>{{ pv_info.id }}</td>
                                            <td>{{ pv_info.create_time }}</td>
                                            <td>{{ pv_info.IPAddress }}</td>
                                            <td>{{ pv_info.IPCity }}</td>
                                            <td>{{ pv_info.CityId }}</td>
                                            <td>{{ pv_info.Agent }}</td>
                                            <td class="td-actions text-center">
                                                <p id="{% url 'stocksys:delete_mystock' %}"
                                                   onclick="delete_info({{ pv_info.id }})"
                                                   title="删除"
                                                   class="btn btn-danger btn-simple btn-xs remove">
                                                    <i class="ti-close"></i>
                                                </p>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block pagescript %}
    <script>
        $(document).ready(function () {
            $('#datatables').DataTable({
                "pagingType": "full_numbers",
                "lengthMenu": [
                    [25],
                    [25]
                ],
                responsive: true,
                language: {
                    search: "_INPUT_",
                    searchPlaceholder: "检索关键字",
                }

            });
            var table = $('#datatables').DataTable();
            table.on('click', '.remove', function (e) {
                $tr = $(this).closest('tr');
                $tr.remove();
            });
        });


        function delete_info(pv_info_id) {
            var token = $.cookie('csrftoken');
            if (pv_info_id === "all") {
                console.log();
                $.ajax({
                    url: "{% url 'managersys:pv_counter' %}",
                    type: "DELETE",
                    headers: {'X-CSRFToken': token},
                    dataType: "json",
                    success: function (data) {
                        if (data['status'] === 200) {
                            $("#datacontent").html("");
                        } else {
                            swal({
                                title: "删除失败",
                                text: "不存在此信息",
                                buttonsStyling: false,
                                confirmButtonClass: "btn btn-success",
                                type: "error"
                            }).catch(swal.noop);
                        }
                    },
                    error: function (e) {
                        console.log(e);
                    }
                });
            }else{
                $.ajax({
                url: "{% url 'managersys:action_pvlog' %}",
                type: "DELETE",
                data: {'id': pv_info_id},
                headers: {'X-CSRFToken': token},
                dataType: "json",
                success: function (data) {
                    if (data['status'] === 200) {
                        $("#pv_info_" + pv_info_id).remove();
                    } else {
                        swal({
                            title: "删除失败",
                            text: "不存在此信息",
                            buttonsStyling: false,
                            confirmButtonClass: "btn btn-success",
                            type: "error"
                        }).catch(swal.noop);
                    }
                },
                error: function (e) {
                    console.log(e);
                }
            });
            }
        }
    </script>
{% endblock %}